// ==UserScript==
// @name         京东Cookie获取
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @run-at      document-end
// @author       You
// @match        *://*.jd.com/*
// @include      https://home.m.jd.com
// @require      https://code.bdstatic.com/npm/leancloud-storage@4.12.0/dist/av-min.js
// @icon         //www.jd.com/favicon.ico
// @grant        GM_xmlhttpRequest
// @grant        GM_cookie
// @grant        GM.cookie
// @grant    GM_setClipboard
// ==/UserScript==

(function() {
    'use strict';

    AV.init({
        appId: "3vYPeRRLQlgqPklfELL3s52C-gzGzoHsz",
  appKey: "G07PLY6GrNgkSVp2uksYA5WH",
  serverURL: "https://jd.jacek.vip"
    });


    const Jd = AV.Object.extend('Jd');
    const query = new AV.Query('Jd');


    let ckArr = {
        'pt_key':'',
        'pt_pin':''
    }



    let addBtn = function(){
        let str = 'pt_key='+ckArr['pt_key']+';pt_pin='+ckArr['pt_pin']+';'

        if(document.location.host==='home.m.jd.com'){
            //
            if(!document.getElementById('getCookie')){
                let btn = document.createElement('input')
                btn.type = 'button'
                btn.id = 'getCookie'
                btn.value = '获取cookie'
                btn.style = 'position: absolute;top: 55px;left: 240px;padding: 5px;color: red;'
                btn.onclick=function(){
                    confirm(str)
                    GM_setClipboard(str)
                    query.equalTo('pt_pin',ckArr['pt_pin']).first().then((jd) => {
                        if(jd){
                            jd.set('pt_key',ckArr['pt_key'])
                            jd.save().then(val=>{
                                console.log('#################更新成功##################')
                                alert('更新成功')
                            },err=>{
                                console.log('#################更新失败##################')
                               alert('更新失败')
                            })
                        }else{
                            jd = new Jd();
                            jd.set('pt_key',ckArr['pt_key'])
                            jd.set('pt_pin',ckArr['pt_pin'])
                            jd.save().then(val=>{
                                console.log('#################保存成功##################')
                                alert('保存成功')
                            },err=>{
                                console.log('#################保存失败##################')
                               alert('保存失败')
                            })
                        }
                    });
                }
                document.body.appendChild(btn)
            }

        }
    }


// GM_cookie.list details supports url, domain, name and path
GM_cookie.list({ url: 'https://home.m.jd.com' }, function(cookies, error) {
    cookies.forEach(c=>{
       ckArr[c.name]=c.value
    })
    addBtn()
});

    let util = {
        getCookie(){
           let ck = document.cookie;
           let ckArr = ck.split(';');

           let map={}

           for(let i=0;i<ckArr.length;i++){
              let arr = ckArr[i].split('=')
              map[arr[0].trim()]=arr[1].trim();
           }

           return map;
        }
    }




})();